#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         GoogleAuthEditlet.py
#
# Copyright:    Copyright (c) 2007, Faulkner Technologies
#
# Author:       James Davies
#
# Description:  This file defines the GoogleAuthEditlet class, that is
#               used to generate authentication tokens for google calendar.
#
#####################################################################

#####################################################################
## KnowledgeEngine Library Imports
from Products.KnowledgeEngine.Common import *
from Products.KnowledgeEngine.Core import *
from Products.KnowledgeEngine.Editlet import Editlet
from Products.KnowledgeEngine.PropertyDefinition import *
from Products.KnowledgeEngine.PropertyTypes import *
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyStructures import *

from Products.KnowledgeEngine.Editlets.Text.TextEditlet import TextEditlet

from Products.PageTemplates.PageTemplateFile import PageTemplateFile



#####################################################################
## Zope Library Imports
# (none)
 
#####################################################################
## Python Library Imports
# (none)

def getSessionToken( authToken ):
    """ Converts a once-off authentication token into a session token """
    
    import gdata
    
    service = gdata.service.GDataService()
    service.auth_token = authToken
    service.UpgradeToSessionToken()
    
    return service._GetAuthToken()
    
    #calendar_service = gdata.calendar.service.CalendarService()
    #calendar_service.auth_token = authToken
    #calendar_service.UpgradeToSessionToken()
    
    #return calendar_service._GetAuthToken()

#####################################################################
## GoogleAuthEditlet Class Definition
class GoogleAuthEditlet(TextEditlet):
    """
        An editlet for editing text properties  
    """
    
    __allow_access_to_unprotected_subobjects__ = True
    
    #GoogleAuthEditlet_render = PageTemplateFile('skins/GoogleAuthEditlet_render.pt', globals())
    
    
    def isEditable(self, propertyDefinition, propertyAccess=None, value=None, isForConstructor=false, REQUEST=None, context=None):
        """
            Returns if the specified propertyDefinition can be
            edited by this editlet.
            
            For GenericObjectChoosers, we must have a model!
        """

        #we can only edit if we are for a chooser
        return propertyDefinition.getFormat() == pf_GOOGLEAUTH
        
        
    def generateEditorFor(self, propertyDefinition, propertyAccess=None, value=None, isForConstructor=false, REQUEST=None, context=None):
        """
            Returns a HTML editlet for the specified property.
        """
        
        scope = "http://docs.google.com/feeds/documents"
        
        if isForConstructor or propertyAccess is None:
            value = propertyDefinition.getDefaultValue()
        else:
            value = propertyAccess.getPropertyValue(propertyDefinition.getIdentity())

            #ensure the value is not none!
            if value is None:
                value = ""
        
        propertyIdentity = propertyDefinition.getIdentity()
        
        brain = dict(locals())
        brain['editlet'] = self
        del brain['self']
                        
        return context.GoogleAuthEditlet_render( **brain )
        

#####################################################################
## Class Initialisation

# Register Editlet Information with the Knowledge Engine
registerEditlet("GoogleAuthEditlet", GoogleAuthEditlet())

#####################################################################
